﻿/**
*┌──────────────────────────────────────────────────────────────┐
*│　描   述：                                                    
*│　作   者：ljx                                              
*│　版   本：1.0                                                 
*│　创建时间：2021/1/20 18:10:54                             
*└──────────────────────────────────────────────────────────────┘

*┌──────────────────────────────────────────────────────────────┐
*│　命名空间: Ldt.WindowsFormsApp                                   
*│　类   名：TreeviewShowJson                                      
*└──────────────────────────────────────────────────────────────┘
*/
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Ldt.WindowsFormsApp
{
    /// <summary>
    /// 将json字符串在treeview上显示,只显示数组和泛型类字段
    /// </summary>
    public static class TreeviewShowJson
    {
        /// <summary>
        /// Json字符串绑定树形控件
        /// </summary>
        /// <param name="treeView">树形控件</param>
        /// <param name="strJson">Json字符串</param>
        public static void BindTreeView(UITreeView treeView, string strJson)
        {
            treeView.Nodes.Clear();
            // json的第一层节点如果是Json对象，就解析Json对象
            if (IsJOjbect(strJson))
            {
                JObject jo = (JObject)JsonConvert.DeserializeObject(strJson);

                foreach (var item in jo)
                {
                    switch (item.Key)
                    {
                        case "DicConfigState":
                        case "ConfigurationFileModelList":
                            break;
                        default:
                            TreeNode tree;
                            if (item.Value.GetType() == typeof(JObject))
                            {
                                // 子对象上树
                                tree = new TreeNode(TableNameToCHName(item.Key));
                                tree.Name = item.Key;
                                AddTreeChildNode(ref tree, item.Value.ToString());
                                treeView.Nodes.Add(tree);
                            }
                            else if (item.Value.GetType() == typeof(JArray))
                            {
                                // 数据对象上树
                                tree = new TreeNode(TableNameToCHName(item.Key));
                                tree.Name = item.Key;
                                AddTreeChildNode(ref tree, item.Value.ToString());
                                treeView.Nodes.Add(tree);
                            }
                            break;
                    }
                }
            }

            // json的第一层节点如果是数组，就解析数组
            if (IsJArray(strJson))
            {
                JArray ja = (JArray)JsonConvert.DeserializeObject(strJson);
                foreach (JObject item in ja)
                {
                    TreeNode tree = new TreeNode();
                    foreach (var itemOb in item)
                    {
                        switch (itemOb.Key)
                        {
                            case "DicConfigState":
                            case "ConfigurationFileModelList":
                                break;
                            default:
                                TreeNode treeOb;
                                if (itemOb.Value.GetType() == typeof(JObject))
                                {
                                    // 数组里面是Json对象
                                    treeOb = new TreeNode(TableNameToCHName(itemOb.Key));
                                    treeOb.Name = itemOb.Key;
                                    AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                                    tree.Nodes.Add(treeOb);

                                }
                                else if (itemOb.Value.GetType() == typeof(JArray))
                                {
                                    // 数组里面继续嵌套这数组
                                    treeOb = new TreeNode(TableNameToCHName(itemOb.Key));
                                    treeOb.Name = itemOb.Key;
                                    AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                                    tree.Nodes.Add(treeOb);
                                }
                                break;
                        }
                    }
                }
            }
            treeView.ExpandAll();
        }
        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="parantNode"></param>
        /// <param name="value"></param>
        public static void AddTreeChildNode(ref TreeNode parentNode, string value)
        {
            if (IsJOjbect(value))
            {
                JObject jo = (JObject)JsonConvert.DeserializeObject(value);
                foreach (var item in jo)
                {
                    TreeNode tree;
                    if (item.Value.GetType() == typeof(JObject))
                    {
                        tree = new TreeNode(TableNameToCHName(item.Key));
                        tree.Name = item.Key;
                        AddTreeChildNode(ref tree, item.Value.ToString());
                        parentNode.Nodes.Add(tree);
                    }
                    else if (item.Value.GetType() == typeof(JArray))
                    {
                        tree = new TreeNode(TableNameToCHName(item.Key));
                        tree.Name = item.Key;
                        AddTreeChildNode(ref tree, item.Value.ToString());
                        parentNode.Nodes.Add(tree);
                    }
                }
            }
            else if (IsJArray(value))
            {
                JArray ja = JArray.Parse(value);
                //JArray ja = (JArray)JsonConvert.DeserializeObject(value);
                foreach (JToken itemvar in ja)
                {
                    TreeNode treeOb;
                    if (itemvar.Type == JTokenType.Array)
                    {
                        //treeOb = new TreeNode(TableNameToCHName(itemOb.Key));
                        //treeOb.Name = itemOb.Key;
                        //AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                        //if (!parentNode.Nodes.ContainsKey(treeOb.Name))
                        //{
                        //    parentNode.Nodes.Add(treeOb);
                        //}

                        //foreach (var item in itemvar)
                        //{

                        //}
                    }
                    else if (itemvar.Type == JTokenType.Object)
                    {

                        //treeOb = new TreeNode(TableNameToCHName(itemvar.Key));
                        //treeOb.Name = itemvar.Key;
                        //AddTreeChildNode(ref treeOb, itemvar.Value.ToString());
                        //if (!parentNode.Nodes.ContainsKey(treeOb.Name))
                        //{
                        //    parentNode.Nodes.Add(treeOb);
                        //}
                    }
                }
                //foreach (JObject item in ja)
                //{
                //    foreach (var itemOb in item)
                //    {
                //        TreeNode treeOb;
                //        if (itemOb.Value.GetType() == typeof(JObject))
                //        {
                //            treeOb = new TreeNode(TableNameToCHName(itemOb.Key));
                //            treeOb.Name = itemOb.Key;
                //            AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                //            if (!parentNode.Nodes.ContainsKey(treeOb.Name))
                //            {
                //                parentNode.Nodes.Add(treeOb);
                //            }
                //        }
                //        else if (itemOb.Value.GetType() == typeof(JArray))
                //        {
                //            treeOb = new TreeNode(TableNameToCHName(itemOb.Key));
                //            treeOb.Name = itemOb.Key;
                //            AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                //            if (!parentNode.Nodes.ContainsKey(treeOb.Name))
                //            {
                //                parentNode.Nodes.Add(treeOb);
                //            }
                //        }
                //    }
                //}
            }
        }
        /// <summary>
        ///  判断是否JOjbect类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static bool IsJOjbect(string value)
        {
            try
            {
                JObject ja = JObject.Parse(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断是否JArray类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static bool IsJArray(string value)
        {
            try
            {
                JArray ja = JArray.Parse(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 设计的表名转成中文释义方便使用者理解
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private static string TableNameToCHName(string tableName)
        {
            string CHName = "";
            switch (tableName.ToLower())
            {
                case "analysisparameter":
                    CHName = "分析要素";
                    break;
                case "sourcedossierinfo":
                    CHName = "卷宗信息";
                    break;
                case "caseentityinfolist":
                    CHName = "案件关联实体信息列表";
                    break;
                case "entityrolelist":
                    CHName = "实体角色信息列表";
                    break;
                case "spokeparagraphtxtlist":
                    CHName = "发言对话内容列表";
                    break;
                case "sd_paragraphtxtlist":
                    CHName = "文书段落内容列表";
                    break;
                case "sd_elementdatalist":
                    CHName = "要素数据";
                    break;
                case "elementspokelist":
                    CHName = "人员信息整合列表";
                    break;
                case "spoketextdialogue":
                    CHName = "人员对话内容列表";
                    break;
                case "casecauseslist":
                    CHName = "案由信息";
                    break;
                default:
                    CHName = tableName;
                    break;
            }
            return CHName;
        }

    }
}
